﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;

namespace PhysicsSimulator
{
    public enum WeightObjectState
    {
        IDLE,
        HOLD,
        DRAG,
        USING,
    }

    class CWeightObject : CGameObject
    {
        Vector2 _idlePosition = Vector2.Zero;
        Vector2 _position = Vector2.Zero;

        int _touchID = -1;

        WeightObjectState _currentState = WeightObjectState.IDLE;

        public WeightObjectState CurrentState
        {
            get
            {
                return _currentState;
            }
            set
            {
                _currentState = value;
            }
        }

        public Vector2 Position
        {
            get
            {
                return _position;
            }
            set
            {
                if (_currentState != WeightObjectState.IDLE)
                {
                    _position.X = value.X;
                    _position.Y = value.Y;
                }
            }
        }

        public Vector2 IdlePosition
        {
            get
            {
                return _idlePosition;
            }
            set
            {
                _idlePosition = value;
                if (_currentState == WeightObjectState.IDLE)
                    _position = value;
            }
        }

        CSprite _objectSprite = null;
        public CSprite Sprite
        {
            get
            {
                return _objectSprite;
            }
        }

        float _weight = 0.0f;
        public float Weight
        {
            get
            {
                return _weight;
            }
        }

        CSprite _weight_bound_sprite = null;
        public void SetWeightBoundSprite(CSprite sprite)
        {
            _weight_bound_sprite = sprite;
        }

        public CWeightObject(SpriteBatch spriteBatch, float weight, CSprite objectSprite) :
            base(spriteBatch)
        {
            _weight = weight;
            _objectSprite = objectSprite;
        }

        public bool Touch(TouchLocation touch)
        {
            Rectangle boundRect = new Rectangle();
            boundRect.X = (int)_position.X - _objectSprite.Width / 2;
            boundRect.Y = (int)_position.Y - _objectSprite.Height / 2;
            boundRect.Width = _objectSprite.Width;
            boundRect.Height = _objectSprite.Height;

            if (touch.Position.X >= boundRect.Left && touch.Position.X <= boundRect.Right
                && touch.Position.Y >= boundRect.Top && touch.Position.Y <= boundRect.Bottom)
            {                
                return true;                
            }
            return false;
        }

        public bool IdleTouch(TouchLocation touch)
        {            
            Rectangle boundRect = new Rectangle();
            boundRect.X = (int)_idlePosition.X - _objectSprite.Width / 2;
            boundRect.Y = (int)_idlePosition.Y - _objectSprite.Height / 2;
            boundRect.Width = _objectSprite.Width;
            boundRect.Height = _objectSprite.Height;

            if (touch.Position.X >= boundRect.Left && touch.Position.X <= boundRect.Right
                && touch.Position.Y >= boundRect.Top && touch.Position.Y <= boundRect.Bottom)
            {
                return true;
            }
            return false;
        }

        public override void Update(GameTimerEventArgs timer)
        {
            base.Update(timer);
        }

        public override void Draw(GameTimerEventArgs timer)
        {
            SpriteBatch.Draw(_objectSprite.Texture, _position, null, Color.White, 0, _objectSprite.Origin, _objectSprite.Scale, SpriteEffects.None, 0);
            if (_currentState != WeightObjectState.IDLE)
            {
                if (_weight_bound_sprite != null)
                {
                    SpriteBatch.Draw(_weight_bound_sprite.Texture,_idlePosition,null,Color.White,0,_weight_bound_sprite.Origin,1,SpriteEffects.None,0);
                }
            }
        }

        public void Reset()
        {
            _currentState = WeightObjectState.IDLE;
            _position = _idlePosition;
        }
    }
}
